﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using Model.Database;

namespace Bll
{
    public partial class DepartmentBll
    {
        /// <summary>
        /// 添加部门及Department_Role关系表
        /// </summary>
        /// <param name="department"></param>
        /// <returns></returns>
        public bool AddDepartmentAndDepartment_Role(Department department)
        {
            CurrentDal.Add(department);
            CurrentDalSession.CurrentDbContext.ChangeTracker.DetectChanges();
            CurrentDalSession.SaveChanges();

            return true;
        }

        /// <summary>
        /// 批量删除部门及部门角色
        /// </summary>
        /// <param name="idArr"></param>
        public void ListDelete(string[] idArr)
        {
            CurrentDal.Delete(a => idArr.Contains(a.DepartmentId));
            CurrentDalSession.SaveChanges();
        }

        /// <summary>
        /// 获取部门分配的角色名
        /// </summary>
        /// <param name="departmentId"></param>
        /// <returns></returns>
        public string GetRoleName(string departmentId)
        {
            string roleName = "";
            List<Role> roleList= GetRole(departmentId);
            if (roleList.Count!=0)
            {
                foreach (var role in roleList)
                {
                    roleName += role.RoleName+",";
                }
            }

            return roleName;
        }

        /// <summary>
        /// 获取部门分配的角色Ids
        /// </summary>
        /// <param name="departmentId"></param>
        /// <returns></returns>
        public string GetRoleIds(string departmentId)
        {
            string roleIds = "";
            List<Role> roleList = GetRole(departmentId);
            if (roleList.Count != 0)
            {
                foreach (var role in roleList)
                {
                    roleIds += role.RoleId + ",";
                }
            }

            return roleIds;
        }

        /// <summary>
        /// 获取部门分配的角色
        /// </summary>
        /// <param name="departmentId"></param>
        /// <returns></returns>
        public List<Role> GetRole(string departmentId)
        {
            //***************************这里使用到了EF的“明确加载”，可以对导航属性进行筛选过滤。并且这里只查询一次数据库,因为currentDbContext已经查询过一次，已经缓存*************************************
            //***************************如果CurrentDbContext之前没有查询过该记录，可以这么做：(这样也只查询一次数据库)
            //***************************Department department = new Department() { DepartmentId = departmentId };
            //***************************CurrentDalSession.CurrentDbContext.Set<Department>().Attach(department);
            List<Role> roleList = new List<Role>();

            Department department = CurrentDal.GetEntity(departmentId);
            CurrentDalSession.CurrentDbContext.Entry(department).Collection(a => a.Roles).Load();

            return department.Roles.ToList();
        }

        /// <summary>
        /// 更新部门及Department_Role关系表
        /// </summary>
        /// <param name="departmentOld"></param>
        /// <returns></returns>
        public bool UpdateDepartmentAndDepartment_Role(Department departmentOld)
        {
            CurrentDal.Update(departmentOld);
            CurrentDalSession.CurrentDbContext.ChangeTracker.DetectChanges();
            CurrentDalSession.SaveChanges();

            return true;
        }

        /// <summary>
        /// 根据Id数组获取部门集合
        /// </summary>
        /// <param name="departmentIdArr"></param>
        /// <returns></returns>
        public List<Department> GetDepartmentByIdArr(string[] departmentIdArr)
        {
            return CurrentDal.GetIQueryable().Where(a => departmentIdArr.Contains(a.DepartmentId)).ToList();
        }
    }
}
